﻿using System;
using System.Net;
using System.Windows;
#if !WinRT
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Interactivity;
using LogoFX.Core;
using EventTrigger = System.Windows.Interactivity.EventTrigger;



namespace LogoFX.Infra.Interactivity.Behaviors
{
    public struct CellState
    {
        public int Index
        {
            get;
            set;
        }

        public DataGridColumn Column
        {
            get;
            set;
        }

        public DataGridRow Row
        {
            get;
            set;
        }

        public DataGrid ParentGrid
        {
            get;
            set;
        }

    }

    public class DataGridCellStyleSelectorBehavior : Behavior<DataGrid>
    {
        #region Dependency Properties

        public static readonly DependencyProperty StyleSelectorProperty =
            DependencyProperty.Register(
                "StyleSelector",
                typeof(IStyleSelector),
                typeof(DataGridCellStyleSelectorBehavior),
                new PropertyMetadata(null));

        /// <summary>
        /// Gets or sets the row style selector.
        /// </summary>
        /// <value>
        /// The row style selector.
        /// </value>
        public IStyleSelector StyleSelector
        {
            get { return (IStyleSelector)GetValue(StyleSelectorProperty); }
            set { SetValue(StyleSelectorProperty, value); }
        }

        #endregion

        /// <summary>
        /// Called after the behavior is attached to an AssociatedObject.
        /// </summary>
        protected override void OnAttached()
        {
            base.OnAttached();

            DataGrid grid = AssociatedObject;
            grid.LoadingRow += OnLoadingRow;
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();

            DataGrid grid = AssociatedObject;
            grid.LoadingRow -= OnLoadingRow;
        }

        /// <summary>
        /// Called when [loading row].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Controls.DataGridRowEventArgs"/> instance containing the event data.</param>
        private void OnLoadingRow(object sender, DataGridRowEventArgs e)
        {
            if (StyleSelector != null)
            {
                for (int i =0; i < AssociatedObject.Columns.Count; i++)
                {
                    var column = AssociatedObject.Columns[i];

                    FrameworkElement fe = column.GetCellContent(e.Row);
                    FrameworkElement result = GetParent(fe, typeof(DataGridCell));
                    DataGridCell cell = (DataGridCell)result;

                    CellState state = new CellState();
                    state.Index = i;
                    state.Column = column;
                    state.Row = e.Row;
                    state.ParentGrid = AssociatedObject;

                    cell.Tag = state;

                    Style style = StyleSelector.SelectStyle(cell, e.Row);

                    if (style != null)
                        cell.Style = style;
                }
            }
        }

        private FrameworkElement GetParent(FrameworkElement child, Type targetType)
        {
            object parent = child.Parent;
            if (parent != null)
            {
                if (parent.GetType() == targetType)
                {
                    return (FrameworkElement)parent;
                }
                else
                {
                    return GetParent((FrameworkElement)parent, targetType);
                }
            }
            return null;
        }
    }
}
#endif